package tgc.edu.yzy.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import tgc.edu.yzy.entity.Admin;
import tgc.edu.yzy.entity.Bj;
import tgc.edu.yzy.entity.Grade;
import tgc.edu.yzy.entity.Student;
import tgc.edu.yzy.entity.Teacher;
import tgc.edu.yzy.service.AdminService;
import tgc.edu.yzy.service.BjService;
import tgc.edu.yzy.service.GradeService;
import tgc.edu.yzy.service.StudentService;
import tgc.edu.yzy.service.TeacherService;


@Controller
@RequestMapping(value="/homepage")
public class LoginController {
	@Autowired
	private StudentService studentservice;
	
	@Autowired
	private TeacherService teacherservice; 
	@Autowired
	private AdminService adminservice;
	@Autowired
	private GradeService gradeservice;
	@Autowired
	private BjService bjservice;
	
	@RequestMapping(value="/index")
	public String index() {
		return "homepage/index";
	}
	
	/***
	 * 登陆
	 * @param username 账号
	 * @param password 密码
	 * @param select   区分登陆人员
	 * @param session  会话
	 * @return
	 */
	@RequestMapping(value="/login",method=RequestMethod.POST)
	public String login(String username,String password,String select,HttpSession session,ModelMap map) {
		Date day=new Date(); 
		SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		System.out.println("\t\r操作类型：登陆");
		if(select.equals("student")) {
			System.out.println("用户类型：学生");
			Student students=studentservice.findBySnAndPassword(username,password);
			if(students==null) {
				System.out.println("学生用户登陆失败，未找到该用户！登陆账号:"+username+"密码:"+password);
				session.removeAttribute("students");
				map.put("gamevery","登录失败！");
				return "homepage/index";
			}else {
				
				System.out.println("学生用户："+students.getName()+"\t在："+df1.format(day)+"成功登陆");
				session.setAttribute("students", students);
				return "redirect:studentjsp";
			}
		}else if(select.equals("teacher")) {
			System.out.println("用户类型：任课老师");
			Teacher teachers=teacherservice.findByTcuserAndTcpassword(username,password);
			if(teachers==null) {
				System.out.println("任课老师登陆失败，未找到该用户！登陆账号:"+username+"密码:"+password);
				session.removeAttribute("teachers");
				map.put("gamevery","登录失败！");
				return "homepage/index";
			}else {
				System.out.println("任课老师："+teachers.getTcname()+"\t在："+df1.format(day)+"成功登陆");
				session.setAttribute("teachers", teachers);
				return "redirect:teacherjsp";
			}
		}else if(select.equals("admin")) {
			System.out.println("用户类型：教务处");
			Admin admins=adminservice.findByUsernameAndPassword(username,password);
			if(admins==null) {
				System.out.println("教务处管理员登陆失败，未找到该用户！登陆账号:"+username+"密码:"+password);
				session.removeAttribute("admins");
				map.put("gamevery","登录失败！");
				return "homepage/index";
			}else {
				System.out.println("教务处管理员："+admins.getName()+"\t在："+df1.format(day)+"成功登陆");
				session.setAttribute("admins", admins);
				return "redirect:adminjsp";
			}
		}
		return "homepage/index";
	}
	/***
	 * 登陆成功
	 * @return
	 */
	@RequestMapping(value="studentjsp")
	public String studentjsp() {
		return "student/studentjsp";
	}
	
	@RequestMapping(value="teacherjsp")
	public String teacherjsp() {
		return "teacher/teacherjsp";
	}
	@RequestMapping(value="adminjsp")
	public String adminjsp() {
		return "admin/adminjsp";
	}
	
	/***
	 * 安全退出
	 * @param session
	 * @return
	 */
	@RequestMapping(value="studentquit")
	public String studentquit(HttpSession session) {
		session.removeAttribute("students");
		return "homepage/index";
	}
	
	@RequestMapping(value="adminquit")
	public String adminquit(HttpSession session) {
		session.removeAttribute("admins");
		return "homepage/index";
	}
	
	@RequestMapping(value="teacherquit")
	public String teacherquit(HttpSession session) {
		session.removeAttribute("teachers");
		return "homepage/index";
	}
	@RequestMapping(value="findall")
	public String findall(ModelMap map) {
		List<Student> student=studentservice.findAll();
		List<Teacher> teacher=teacherservice.findAll();
		List<Grade> grade=gradeservice.findAll();
		List<Bj> bj=bjservice.findAll();
		List<Admin> admin=adminservice.findAll();
		map.put("teacher", teacher);
		map.put("admin", admin);
		map.put("grade", grade);
		map.put("bj", bj);
		map.put("student", student);
		return "homepage/help";
	}
}
